package com.jy.appmarket.cards;

import it.gmariotti.cardslib.library.internal.Card;

import java.util.ArrayList;
import java.util.List;

import android.content.Context;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.Rect;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.Toast;

import com.jy.app.market.idata.data.Apk.ScreenShots;
import com.jy.appmarket.R;
import com.jy.appmarket.activity.ScreenShotsActivity;
import com.jy.appmarket.download.DownloadActivity;
import com.jy.appmarket.utils.LogUtil;
import com.nostra13.universalimageloader.core.ImageLoader;
import com.nostra13.universalimageloader.core.listener.SimpleImageLoadingListener;

/**
 * 类说明：
 * 
 * @author Baker.li
 * @date 2015年8月21日
 * @version 1.0
 */

public class AppDetailScreenshotsUICard extends Card {
	private ScreenShots mScreenShots;
	private DownloadActivity mDLActivity;

	public AppDetailScreenshotsUICard(Context context, ScreenShots screenShots) {
		super(context, R.layout.appdetail_screenshots_card_layout);
		mDLActivity = (DownloadActivity) context;
		mScreenShots = screenShots;
		setShadow(false);
	}

	@Override
	public void setupInnerViewElements(ViewGroup parent, View view) {
		RecyclerView gallery = parent
				.findViewById(R.id.appdetail_screenshots_recyclerview);
		gallery.setLayoutManager(new LinearLayoutManager(getContext(),
				LinearLayoutManager.HORIZONTAL, false));

		gallery.setAdapter(new GalleryAdapter(mScreenShots.getSmall()));
	}

	@Override
	public int getType() {
		return 6;
	}

	private static class MyHolder extends RecyclerView.ViewHolder {
		public ImageView image;

		public MyHolder(View view) {
			super(view);
			image = view.findViewById(R.id.appdetail_gallery_item_image);
		}
	}

	private class GalleryAdapter extends RecyclerView.Adapter<MyHolder> {

		private List<String> mData;

		public GalleryAdapter(List<String> list) {
			mData = list;
		}

		@Override
		public int getItemCount() {
			return mData.size();
		}

		@Override
		public void onViewRecycled(MyHolder holder) {
			super.onViewRecycled(holder);
		}

		@Override
		public void onBindViewHolder(final MyHolder vh, final int position) {
			ImageLoader.getInstance().displayImage(mData.get(position), vh.image,
					new SimpleImageLoadingListener() {
						@Override
						public void onLoadingComplete(String imageUri, View view, Bitmap loadedImage) {

							LogUtil.e("ImageLoader", "onLoadingComplete:[" + loadedImage.getWidth()
									+ "," + loadedImage.getHeight() + "]");

							float width = 1.0f * loadedImage.getWidth() * view.getHeight()
									/ loadedImage.getHeight();
							ViewGroup.LayoutParams lp = view.getLayoutParams();
							lp.width = (int) width;
							view.setLayoutParams(lp);
							// view.setVisibility(View.VISIBLE);
						}
					});
			vh.image.setOnClickListener(new View.OnClickListener() {
				@Override
				public void onClick(View v) {
					ScreenShots ss = mScreenShots;
					if (ss != null && ss.getSmall() != null && ss.getNormal() != null) {

						ArrayList<String> small = new ArrayList<String>();
						for (String s : ss.getSmall()) {
							small.add(s);
						}
						ArrayList<String> normal = new ArrayList<String>();
						for (String s : ss.getNormal()) {
							normal.add(s);
						}

						Intent intent = new Intent(v.getContext(), ScreenShotsActivity.class);
						Rect rect = new Rect();
						v.getGlobalVisibleRect(rect);
						intent.setSourceBounds(rect);
						intent.putStringArrayListExtra(ScreenShotsActivity.EXTRA_SMALL_LIST, small);
						intent.putStringArrayListExtra(ScreenShotsActivity.EXTRA_NORMAL_LIST,
								normal);
						intent.putExtra(ScreenShotsActivity.EXTRA_INDEX, position);
						ImageView image = (ImageView) v;
						ScreenShotsActivity.DrawableHolder.index = position;
						ScreenShotsActivity.DrawableHolder.drawable = image.getDrawable();
						v.getContext().startActivity(intent);
						mDLActivity.overridePendingTransition(R.anim.zoom_in, R.anim.zoom_out);
					} else {
						Toast.makeText(v.getContext(), "无法查看大图", Toast.LENGTH_SHORT).show();
					}
				}
			});
		}

		@Override
		public MyHolder onCreateViewHolder(ViewGroup viewGroup, int position) {
			View view = LayoutInflater.from(viewGroup.getContext()).inflate(
					R.layout.appdetail_gallery_item, viewGroup, false);
			MyHolder holder = new MyHolder(view);
			return holder;
		}
	}
	
	@Override
	public int getMarginTop(){
		return 0; //返回0，则在顶部没有空隙
	}
}